package com.jxb.picker;

import android.content.Context;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.view.TimePickerView;

import java.text.SimpleDateFormat;

/**
 * @author jinxb
 * date     : 2021/5/19
 * desc     :
 */
public class TimePickViewUtils {
    private final boolean[] TYPE_YMD = new boolean[]{true, true, true, false, false, false};
    private final boolean[] TYPE_YMDHM = new boolean[]{true, true, true, true, true, false};
    private final boolean[] TYPE_YMDHMS = new boolean[]{true, true, true, true, true, true};
    private final boolean[] TYPE_HMS = new boolean[]{false, false, false, true, true, true};

    private TimePickerView build;

    private TimePickViewUtils() {

    }

    public static TimePickViewUtils getInstance() {
        return Holder.sInstance;
    }

    private static class Holder {
        public static TimePickViewUtils sInstance = new TimePickViewUtils();
    }

    /* 以Bottom形式显示 */

    /**
     * 显示时间弹框（bottom）
     */
    public void showTimePickViewBottom(Context context, boolean[] types, String title, SimpleDateFormat format,
                                       OnTimeSelectedListener listener) {
        showTimePickView(context, types, title, false, format, listener);
    }

    /**
     * 显示时间弹框（bottom，年月日）
     */
    public void showTimePickViewBottomYMD(Context context, String title, String splice,
                                          OnTimeSelectedListener listener) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy"+splice+"MM"+splice+"dd");
        showTimePickView(context, TYPE_YMD, title, false, format, listener);
    }

    /**
     * 显示时间弹框（bottom，时分秒）
     */
    public void showTimePickViewBottomHMS(Context context, String title, String splice,
                                          OnTimeSelectedListener listener) {
        SimpleDateFormat format = new SimpleDateFormat("HH"+splice+"mm"+splice+"ss");
        showTimePickView(context, TYPE_HMS, title, false, format, listener);
    }

    /**
     * 显示时间弹框（bottom，年月日时分秒）
     */
    public void showTimePickViewBottomYMDHMS(Context context, String title, String splice,
                                             OnTimeSelectedListener listener) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy"+splice+"MM"+splice+"dd HH"+splice+"mm"+splice+"ss");
        showTimePickView(context, TYPE_YMDHMS, title, false, format, listener);
    }

    /**
     * 显示时间弹框（bottom，年月日时分）
     */
    public void showTimePickViewBottomYMDHM(Context context, String title, String splice,
                                            OnTimeSelectedListener listener) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy"+splice+"MM"+splice+"dd HH"+splice+"mm");
        showTimePickView(context, TYPE_YMDHM, title, false, format, listener);
    }

    /* 以Dialog形式显示 */

    /**
     * 显示时间弹框（dialog）
     */
    public void showTimePickViewDialog(Context context, boolean[] types, String title, SimpleDateFormat format,
                                       OnTimeSelectedListener listener) {
        showTimePickView(context, types, title, true, format, listener);
    }

    /**
     * 显示时间弹框（dialog，年月日）
     */
    public void showTimePickViewDialogYMD(Context context, String title, String splice,
                                          OnTimeSelectedListener listener) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy"+splice+"MM"+splice+"dd");
        showTimePickView(context, TYPE_YMD, title, true, format, listener);
    }

    /**
     * 显示时间弹框（dialog，年月日时分秒）
     */
    public void showTimePickViewDialogYMDHMS(Context context, String title, String splice,
                                             OnTimeSelectedListener listener) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy"+splice+"MM"+splice+"dd HH"+splice+"mm"+splice+"ss");
        showTimePickView(context, TYPE_YMDHMS, title, true, format, listener);
    }

    /**
     * 显示时间弹框（dialog，时分秒）
     */
    public void showTimePickViewDialogHMS(Context context, String title, String splice,
                                          OnTimeSelectedListener listener) {
        SimpleDateFormat format = new SimpleDateFormat("HH"+splice+"mm"+splice+"ss");
        showTimePickView(context, TYPE_HMS, title, true, format, listener);
    }

    /**
     * 显示时间弹框（bottom，年月日时分）
     */
    public void showTimePickViewDialogYMDHM(Context context, String title, String splice,
                                            OnTimeSelectedListener listener) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy"+splice+"MM"+splice+"dd HH"+splice+"mm");
        showTimePickView(context, TYPE_YMDHM, title, true, format, listener);
    }

    private void showTimePickView(Context context, boolean[] type, String title, boolean isDialog,
                                  SimpleDateFormat format,
                                  OnTimeSelectedListener listener) {
        dismiss();
        int titleColor = context.getResources().getColor(R.color.picker_cancel_color);
        int submitColor = context.getResources().getColor(R.color.picker_cancel_color);
        int cancelColor = context.getResources().getColor(R.color.picker_cancel_color);
        int titleSize = context.getResources().getDimensionPixelSize(R.dimen.pickerview_textsize);
        build = new TimePickerBuilder(context, (date, v) -> listener.onTimeSelected(format.format(date)))
                .setType(type)// 默认全部显示
                .setCancelText("取消")//取消按钮文字
                .setSubmitText("确认")//确认按钮文字
                .setTitleSize(titleSize)//标题文字大小
                .setTitleText(title)//标题文字
                .isDialog(isDialog)
                .setOutSideCancelable(false)//点击屏幕，点在控件外部范围时，是否取消显示
                .isCyclic(false)//是否循环滚动
                .setTitleColor(titleColor)//标题文字颜色
                .setSubmitColor(submitColor)//确定按钮文字颜色
                .setCancelColor(cancelColor)//取消按钮文字颜色
                .build();
        build.show();
    }

    private void dismiss() {
        if (build != null) {
            build.dismiss();
            build = null;
        }
    }

    public interface OnTimeSelectedListener {
        void onTimeSelected(String date);
    }

}
